﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolManagerMVC.BusinessAbstract.Interfaces;
using SchoolManagerMVC.Repository.EntityFramework;
using SchoolManagerMVC.Web.Areas.Courses.ViewModels;

namespace SchoolManagerMVC.Web.Areas.Courses.Controllers
{
    public class EnrollmentController : Controller
    {
        /// <summary>
        /// Course Area
        /// </summary>
        private IRepository _repo;
        public EnrollmentController(IRepository repo)
        {
            _repo = repo;
            if (_repo == null)
                throw new NullReferenceException();
        }
        /*
        public ActionResult Index(int? id)
        {
            if (id == null)
                return RedirectToAction("Index", "Home", null);


            var studentsInCourse = _repo.GetWhere<StudentInCourse>(e => e.CourseID == id);

            if (studentsInCourse == null)
                return RedirectToAction("Index", "Home", null);

            List<StudentInCourseViewModel> studentsInCoursesList = new List<StudentInCourseViewModel>();
            foreach (var curr in studentsInCourse)
            {
                var student = _repo.GetWhere<Student>(e => e.StudentID == curr.StudentID).FirstOrDefault();

                studentsInCoursesList.Add(new StudentInCourseViewModel()
                {
                    CourseId = (int)id,
                    Course = _repo.GetWhere<Course>(e => e.CourseID == curr.CourseID).FirstOrDefault().CourseName,
                    Student = student.FirstLast,
                    StudentID = student.StudentID,
                    UniqueID = student.UniqueID
                });
            }

            if (studentsInCoursesList.Count == 0)
                studentsInCoursesList.Add(new StudentInCourseViewModel()
                {
                    CourseId=(int)id
                });

            return View(studentsInCoursesList);
        }

        public ActionResult Create(int? id)
        {
            if (id == null)
                return RedirectToAction("Index", "Home", null);

            ViewBag.studentList = _repo.GetAll<Student>();
            
            return View(new StudentInCourse()
            {
                CourseID = (int)id
            });
        }

        [HttpPost]
        public ActionResult Create([Bind(Exclude="EnrollmentID")]StudentInCourse studentInCourse)
        {
            ViewBag.studentList = _repo.GetAll<Student>();
            if (!ModelState.IsValid)
            {
                return View("Create", studentInCourse);
            }
            if (_repo.GetSingle<StudentInCourse>(e => e.CourseID == studentInCourse.CourseID && e.StudentID == studentInCourse.StudentID) != null)
            {
                ModelState.AddModelError(string.Empty, "Student is already enrolled in Course");
                return View("Create", studentInCourse);
            }
            
            _repo.Add<StudentInCourse>(studentInCourse);
            return RedirectToAction("Index", "Enrollment", new { id = studentInCourse.CourseID });
        }

        [HttpPost]
        public ActionResult Remove(StudentInCourseViewModel studentInCourse)
        {
            var request = _repo.GetWhere<StudentInCourse>(e => e.StudentID == studentInCourse.StudentID && e.CourseID == studentInCourse.CourseId).FirstOrDefault();
            _repo.Remove<StudentInCourse>(request);

            return RedirectToAction("Index","Enrollment", new { id = studentInCourse.CourseId });
        }
         * */
    }
}
